// pages/details/details.js
const config = require("../../utils/config.js")
const req = require("../../utils/request.js")
const app = getApp()
var y =1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",            //名字
    goods_brief:"",     //描述
    primary_pic_url:[], //图片
    goods_number:"",    //月销量
    goods_unit:"",      //单位
    extra_price:"",     //运费
    comment:"" ,        //评价
    count:"",            //评价人数
    add_time:"",         //评价时间
    attributeN:[],        //产品参数
    attributeV:[],        //产品详细
    anima: "",
    anima1: "",           
    goodsId:"",            //商品ID
    goodN:1,              //数量
    productId:"",
    token:"" ,
    anima:"",//动画   
    countS:"",//库存    
    retailP:"",//售价 
    num:2
  },
  getData:function(){
    var that = this 
    var Id = app.globalData.id
    var idx = app.globalData.idx
    var id = Id[idx]
    console.log(id)
    var primary_pic_url = that.data.primary_pic_url
    var comment = that.data.comment
    var add_time = that.data.add_time
    var attributeN = that.data.attributeN
    var attributeV = that.data.attributeV
    req.request(config.brand,"GET",{id:id},function(res){
      console.log(res.data.data.info.retail_price)
      console.log(res)
      // 获取库存数量与售价
      that.setData({
        countS: res.data.data.info.goods_number,
        retailP: res.data.data.info.retail_price
      })

      if (res.data.data.gallery.length == 0){
        primary_pic_url[0] = res.data.data.info.primary_pic_url
      }else{
        for (var i = 0; i < res.data.data.gallery.length; i++) {
          primary_pic_url[i] = res.data.data.gallery[i].img_url
        }
      }
      if(res.data.data.comment.count == 0){
        comment = "暂无评价"
        add_time = "0000-00-00 00:00:00"
      }else{
        comment = res.data.data.comment.data.content
        add_time = res.data.data.comment.data.add_time
      }
      for (var j = 0; j < res.data.data.attribute.length;j++){
        attributeN[j] = res.data.data.attribute[j].name
        attributeV[j] = res.data.data.attribute[j].value
      }
      that.setData({
        name: res.data.data.info.name,
        goods_brief: res.data.data.info.goods_brief,
        primary_pic_url: primary_pic_url,
        goods_number: res.data.data.info.goods_number,
        goods_unit: res.data.data.info.goods_unit,
        extra_price: res.data.data.info.extra_price,
        comment:comment,
        count:res.data.data.comment.count,
        add_time: add_time,
        attributeN:attributeN,
        attributeV:attributeV,
        goodsId: res.data.data.productList[0].goods_id,
        // goodN: res.data.data.productList[0].goods_number,
        productId: res.data.data.productList[0].id
      })
    })
  },
  tianjia:function(){
    this.aniamti=wx.createAnimation({
      duration:200
    })
    this.aniamti.bottom(0).step();
    this.setData({
      anima: this.aniamti.export()
    })
    
  },
  // 增加商品数量
  increase(){
    y++;
    var retailPs = this.data.retailP*y;
    console.log((retailPs.toString()).length)
    if (this.data.countS == 0 || y > this.data.countS+1){
      wx.showToast({
        title: '库存不足',
        icon: "none"
      })
      this.setData({
        num: 2
      })
    }else{
     this.setData({
       goodN:y,
       retailP: (retailPs) 
     })
    }
  },
  // 减少商品数量
  reduce(){
    if (this.data.countS == 0 || y<2 ){
      wx.showToast({
        title: '库存不足',
        icon:"none"
      })
      this.setData({
        num:2
      })
    }else {
      y--;
      var retailPs = this.data.retailP /( y+1);
      this.setData({
        goodN: y,
        retailP: retailPs
      })
    }
  },
  addShopp(){
    var that = this;
    console.log(that.data.goodsId);
    console.log(that.data.goodN);
    console.log(that.data.productId);
    wx.request({
      url: config.cart,
      method: "POST",
      header: {
        "X-Nideshop-Token": that.data.token
      },
      data: {
        goodsId: that.data.goodsId,
        number: that.data.goodN,
        productId: that.data.productId
      },
      success: function (res) {
        console.log(res)
        wx.switchTab({
          url: '../shopping/cart/cart',
          success: function (res) {
            console.log('成功')
          },
          fail: function (res) {
            console.log('失败')
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  chuxian: function () {
    var ani1 = wx.createAnimation({
      duration: 500,
      timingFunction: "linear",
      delay: 100,
      transformOrigin: "50% 50%"
    })
    ani1.top(0).step();
    this.setData({
      anima1: ani1.export()
    })
  },
  del: function (event) {
    // event.stopPropagation
    var ani2 = wx.createAnimation({
      duration: 500,
      timingFunction: "linear",
      delay: 100,
      transformOrigin: "50% 50%"
    })
    ani2.top("100%").step();
    this.setData({
      anima1: ani2.export()
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
    var that=this;
    wx.getStorage({
      key: 'useof',
      success: function(res) {
        // console.log(res.data.token)
        that.setData({
          token: res.data.token
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})